package com.demo.service;

import com.demo.beans.PageQuery;
import com.demo.beans.PageResult;
import com.demo.common.RequestHolder;
import com.demo.dao.OriginMapper;
import com.demo.exception.ParamException;
import com.demo.model.Origin;
import com.demo.param.OriginParam;
import com.demo.util.BeanValidator;


import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by caiqibin on 18/3/17.
 */
@Service
public class OriginService {
    @Resource
    private OriginMapper originMapper;

    public void save(OriginParam param){
        BeanValidator.check(param);
        checkDataParam(param);

        Origin origin = Origin.builder().shopNumber(param.getShopNumber()).shopName(param.getShopName())
                .quantity(param.getQuantity()).amount(param.getAmount())
                .shopPrice(param.getShopPrice()).shopBar(param.getShopBar()).shopCompany(param.getShopCompany())
                .loginIp(getLoginIp()).loginDate(new Date()).build();

        originMapper.insertSelective(origin);

    }

    public void update(OriginParam param) {
        BeanValidator.check(param);
        checkDataParam(param);

        Origin before = originMapper.selectByPrimaryKey(param.getOriginId());
        Preconditions.checkNotNull(before, "待更新的商品不存在");

        Origin after = Origin.builder().originId(param.getOriginId()).shopNumber(param.getShopNumber()).shopName(param.getShopName())
                .quantity(param.getQuantity()).amount(param.getAmount())
                .shopPrice(param.getShopPrice()).shopBar(param.getShopBar()).shopCompany(param.getShopCompany())
                .loginIp(getLoginIp()).loginDate(new Date()).build();

        originMapper.updateByPrimaryKeySelective(after);

    }

    private void checkDataParam(OriginParam param){
        if(checkOriginNumberExist(param.getShopNumber(),param.getOriginId())){
            throw new ParamException("商品号码已经存在");
        }

        if (checkOriginNameExist(param.getShopName(),param.getOriginId())) {
            throw new ParamException("商品名称已经存在");
        }

        if (checkOriginBarExist(param.getShopBar(),param.getOriginId())) {
            throw new ParamException("商品条码已经存在");
        }
    }

    public String getLoginIp(){
        String ip = "";
        if(RequestHolder.getCurrentRequest()!= null){
            ip = RequestHolder.getCurrentRequest().getRemoteAddr();
            System.out.println(ip);
        }
        return ip;
    }

    public List<Origin> getAll() {
        return originMapper.getAll();
    }

    public void delete(Integer id){
        originMapper.deleteByPrimaryKey(id);
    }

    private boolean checkOriginNameExist(String name,Integer id) {
        return originMapper.countByOriginNameAndId(name,id) > 0;
    }
    private boolean checkOriginBarExist(String name,Integer id) {
        return originMapper.countByOriginBarAndId(name,id) > 0;
    }
    private boolean checkOriginNumberExist(String originNumber,Integer id) {
        return originMapper.countByOriginNumberAndId(originNumber,id) > 0;
    }

    public PageResult<Origin> getPageByAll(PageQuery page) {
        BeanValidator.check(page);
        int count = originMapper.countByAll();
        if (count > 0) {
            List<Origin> originList = originMapper.getPageByAll(page);
            return PageResult.<Origin>builder().data(originList).total(count).build();
        }
        return PageResult.<Origin>builder().build();
    }


}
